package zy.blue7.flowable.bpmn;

import org.flowable.engine.*;
import org.flowable.engine.impl.cfg.StandaloneProcessEngineConfiguration;
import org.junit.Test;

import java.io.InputStream;

public class ProcessEngineTest {

    /**
     * flowable 配置风格--引擎与引擎配置类的说明
     * <p>
     * 1.引擎类是通过引擎配置类构造出来的
     * 2.引擎配置类 是通过xml 定义的
     * 3.引擎配置类是我们开发人员根据自己需要来配置的
     * <p>
     * todo
     * 4.一系列的服务类是通过引擎配置类构造的，并非引擎类构造的
     * <p>
     * flowable 配置风格--相关服务类职责
     * <p>
     * todo RepositoryService  ----》 操作流程定义
     * todo RuntimeService     ----》操作流程实例
     * todo TaskService        ----》操作任务
     * todo IdentityService    -----》操作用户或者组
     * todo HistoryService     -----》操作历史数据
     * todo DynamicBpmnService ----》动态bpmn服务
     * todo ManagementService  -----》 执行cmd和 job 相关的
     * todo FormService        操作流程表单的
     * <p>
     * <p>
     * todo flowable 配置风格---常用术语约定
     * ProcessEngines  流程引擎管理类
     * ProcessEngine   流程引擎类
     * ProcessEngineImpl  流程引擎实现类
     * ProcessEngineConfiguration  流程引擎配置类
     * ProcessEngineConfigurationImpl  流程引擎配置实现类
     * ProcessEngineInfo     流程引擎信息类
     * <p>
     * <p>
     * <p>
     * <p>
     * todo 流程引擎管理类 ProcessEngines中的属性
     * protected static Map<String, ProcessEngine> processEngines = new HashMap();
     * key 流程引擎名称  值：流程引擎对象
     * protected static Map<String, EngineInfo> processEngineInfosByName = new HashMap();
     * key 流程引擎名称  值：流程引擎信息类实例对象
     * protected static Map<String, EngineInfo> processEngineInfosByResourceUrl = new HashMap();
     * key 构造流程引擎的资源名称   值：流程引擎信息类实例对象
     * protected static List<EngineInfo> processEngineInfos = new ArrayList();
     * 存储 流程引擎信息类实例对象
     * <p>
     * <p>
     * todo 配置文件 名称flowable.cfg.xml 是写死的，在代码中
     */
    ProcessEngine processEngine;

    @Test
    public void testProcessEngine() {

        /**
         * 获取默认的流程引擎类
         */
        //todo 主要是通过flowable的风格的配置文件  创建一个 {@see org.flowable.engine.impl.cfg.StandaloneProcessEngineConfiguration} 配置类
        //todo 该配置类 中创建了 多个service， 然后赋值给processengine
        processEngine = ProcessEngines.getDefaultProcessEngine();

        System.out.println("默认的流程引擎类 " + processEngine);

        //todo 获取的service

        DynamicBpmnService dynamicBpmnService = processEngine.getDynamicBpmnService();
        System.out.println(dynamicBpmnService);

        FormService formService = processEngine.getFormService();
        System.out.println(formService);

        HistoryService historyService = processEngine.getHistoryService();
        System.out.println(historyService);

        IdentityService identityService = processEngine.getIdentityService();
        System.out.println(identityService);

        ManagementService managementService = processEngine.getManagementService();
        System.out.println(managementService);

        RepositoryService repositoryService = processEngine.getRepositoryService();
        System.out.println(repositoryService);

        RuntimeService runtimeService = processEngine.getRuntimeService();
        System.out.println(runtimeService);

        TaskService taskService = processEngine.getTaskService();
        System.out.println(taskService);

        ProcessEngineConfiguration processEngineConfiguration = processEngine.getProcessEngineConfiguration();
        System.out.println(processEngineConfiguration);

        String name = processEngine.getName();
        System.out.println("流程引擎的名字：" + name);

    }


    /**
     * todo 编码的形式创建流程引擎
     * 手动创建一个流程引擎配置类和流程引擎类
     */
    @Test
    public void buildProcessEngine() {
        ProcessEngineConfiguration standaloneProcessEngineConfiguration = ProcessEngineConfiguration.createStandaloneProcessEngineConfiguration();
        standaloneProcessEngineConfiguration.setJdbcDriver("com.mysql.jdbc.Driver");
        standaloneProcessEngineConfiguration.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/flowable-process?characterEncoding=UTF-8");
        standaloneProcessEngineConfiguration.setJdbcUsername("root");
        standaloneProcessEngineConfiguration.setJdbcPassword("2868463718");

        //todo 构建流程引擎
        ProcessEngine processEngine = standaloneProcessEngineConfiguration.buildProcessEngine();
        System.out.println("手动编码创建的流程引擎配置类，再创建流程引擎类：" + processEngine);


        /**
         *
         *  todo 创建流程引擎配置类 方法---》org.flowable.engine.ProcessEngineConfiguration类中的方法
         *
         *  public static ProcessEngineConfiguration createProcessEngineConfigurationFromResourceDefault() {
         *         return createProcessEngineConfigurationFromResource("flowable.cfg.xml", "processEngineConfiguration");
         *     }
         *
         *     public static ProcessEngineConfiguration createProcessEngineConfigurationFromResource(String resource) {
         *         return createProcessEngineConfigurationFromResource(resource, "processEngineConfiguration");
         *     }
         *
         *     public static ProcessEngineConfiguration createProcessEngineConfigurationFromResource(String resource, String beanName) {
         *         return (ProcessEngineConfiguration)BeansConfigurationHelper.parseEngineConfigurationFromResource(resource, beanName);
         *     }
         *
         *     public static ProcessEngineConfiguration createProcessEngineConfigurationFromInputStream(InputStream inputStream) {
         *         return createProcessEngineConfigurationFromInputStream(inputStream, "processEngineConfiguration");
         *     }
         *
         *     public static ProcessEngineConfiguration createProcessEngineConfigurationFromInputStream(InputStream inputStream, String beanName) {
         *         return (ProcessEngineConfiguration)BeansConfigurationHelper.parseEngineConfigurationFromInputStream(inputStream, beanName);
         *     }
         *
         *     public static ProcessEngineConfiguration createStandaloneProcessEngineConfiguration() {
         *         return new StandaloneProcessEngineConfiguration();
         *     }
         *
         *
         */
    }

    @Test
    public void buildProcessEngine2() {
        ProcessEngineConfiguration standaloneProcessEngineConfiguration = new StandaloneProcessEngineConfiguration();
        standaloneProcessEngineConfiguration.setJdbcDriver("com.mysql.jdbc.Driver");
        standaloneProcessEngineConfiguration.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/flowable-process?characterEncoding=UTF-8");
        standaloneProcessEngineConfiguration.setJdbcUsername("root");
        standaloneProcessEngineConfiguration.setJdbcPassword("2868463718");

        //todo 构建流程引擎
        ProcessEngine processEngine = standaloneProcessEngineConfiguration.buildProcessEngine();
        System.out.println("手动编码创建的流程引擎配置类，再创建流程引擎类：" + processEngine);
    }

    /**
     * 通过输入流创建流程引擎
     */
    @Test
    public void buildProcessEngine3() {
        //todo 输入流是配置文件
        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("flowable.cfg.xml");
        ProcessEngineConfiguration processEngineConfiguration = ProcessEngineConfiguration.createProcessEngineConfigurationFromInputStream(inputStream);
        ProcessEngine processEngine = processEngineConfiguration.buildProcessEngine();
        System.out.println("手动编码创建的流程引擎配置类，再创建流程引擎类：" + this.processEngine);
    }

    @Test
    public void buildProcessEngine4() {
        //todo 输入流是配置文件
        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("flowable.cfg.xml1");
        //todo 自定义流程引擎配置类的名字，，注意 这里等于配置文件中的流程引擎配置类的名字
        String beanName = "processEngineConfiguration1";
        ProcessEngineConfiguration processEngineConfiguration = ProcessEngineConfiguration.createProcessEngineConfigurationFromInputStream(inputStream, beanName);
        ProcessEngine processEngine = processEngineConfiguration.buildProcessEngine();
        System.out.println("手动编码创建的流程引擎配置类，再创建流程引擎类：" + this.processEngine);
    }

    @Test
    public void buildProcessEngine5() {
        //todo 流程引擎配置类的名字和 配置文件的名字都是固定死了
        ProcessEngineConfiguration processEngineConfiguration = ProcessEngineConfiguration.createProcessEngineConfigurationFromResourceDefault();
        ProcessEngine processEngine = processEngineConfiguration.buildProcessEngine();
        System.out.println("手动编码创建的流程引擎配置类，再创建流程引擎类：" + this.processEngine);
    }

    @Test
    public void buildProcessEngine6() {
        //todo 流程引擎配置类的名字是固定死了
        String resource = "flowable.cfg.xml";
        ProcessEngineConfiguration processEngineConfiguration = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource(resource);
        ProcessEngine processEngine = processEngineConfiguration.buildProcessEngine();
        System.out.println("手动编码创建的流程引擎配置类，再创建流程引擎类：" + this.processEngine);
    }

    @Test
    public void buildProcessEngine7() {
        //todo 流程引擎配置类的名字是固定死了
        String resource = "flowable.cfg.xml";
        String beanName = "processEngineConfiguration";
        ProcessEngineConfiguration processEngineConfiguration = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource(resource, beanName);
        ProcessEngine processEngine = processEngineConfiguration.buildProcessEngine();
        System.out.println("手动编码创建的流程引擎配置类，再创建流程引擎类：" + this.processEngine);
    }


    @Test
    public void test1() {
        /**
         * 扩展 flowable 配置风格的 功能
         *  todo 1.设置自带 的开关属性
         *  todo 2.继承流程引擎配置类，再扩展
         */
    }
}
